use std::sync::atomic::AtomicU16;
use std::sync::LazyLock;
use time::formatting::Formattable;
use time::{format_description, OffsetDateTime};
use tracing::error;

use crate::utils::time::current_data_time_with_zone;

//程序的全局变量，先放在这里备用的

pub static DEFAULT_DATA_FORMAT: LazyLock<Vec<format_description::FormatItem<'_>>> =
	LazyLock::new(|| {
		format_description::parse("[year]-[month]-[day]")
			.expect("parse DEFAULT_DATA_FORMAT date format error")
	});

pub static DEFAULT_DATATIME_FORMAT: LazyLock<Vec<format_description::FormatItem<'_>>> =
	LazyLock::new(|| {
		format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second]").unwrap()
	});

pub static EMPTY_STR: LazyLock<String> = LazyLock::new(|| String::default());

///直接格式化当前时间，使用默认的格式
#[inline]
pub fn format_current_dt_by_default() -> String {
	let dt = current_data_time_with_zone();

	format_date_time(&dt, &DEFAULT_DATA_FORMAT)
}

///格式化字符串
#[inline]
pub fn format_date_time(dt: &OffsetDateTime, format: &(impl Formattable + ?Sized)) -> String {
	match dt.format(format) {
		Ok(s) => s,
		Err(e) => {
			error!("格式化日期出现异常。格式：{}", e);

			OffsetDateTime::now_utc()
				.format(&DEFAULT_DATA_FORMAT)
				.unwrap_or("".to_owned())
		}
	}
}

pub static CALLOUT_ID: AtomicU16 = AtomicU16::new(0);
